<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI 电脑管家 - 小助手</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: #1a1a1a;
            color: #ffffff;
            height: 100vh;
            display: flex;
            flex-direction: column;
            overflow: hidden;
            margin: 0;
            padding: 0;
        }

        .header {
            background: #2a2a2a;
            border-bottom: 1px solid #333;
            padding: 1rem;
            display: flex;
            align-items: center;
            justify-content: space-between;
            -webkit-app-region: drag; /* 允许拖拽窗口 */
        }

        .header-left {
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }

        .ai-avatar {
            width: 32px;
            height: 32px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1rem;
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.05); }
        }

        .header-title {
            font-size: 1rem;
            font-weight: 600;
        }

        .header-actions {
            display: flex;
            gap: 0.5rem;
        }

        .action-btn {
            width: 28px;
            height: 28px;
            background: #333;
            border: 1px solid #444;
            border-radius: 6px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s ease;
            color: #666;
            font-size: 0.8rem;
            -webkit-app-region: no-drag; /* 禁止拖拽 */
        }

        .action-btn:hover {
            background: #444;
            color: #ffffff;
        }

        .main-content {
            flex: 1;
            display: flex;
            flex-direction: column;
            padding: 1rem;
            gap: 1rem;
            overflow-y: auto;
            min-height: 0;
        }

        .quick-actions {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 0.75rem;
        }

        .quick-action {
            background: #2a2a2a;
            border: 1px solid #333;
            border-radius: 10px;
            padding: 1rem;
            cursor: pointer;
            transition: all 0.3s ease;
            text-align: center;
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 0.5rem;
        }

        .quick-action:hover {
            background: #333;
            border-color: #444;
            transform: translateY(-2px);
        }

        .quick-action-icon {
            font-size: 1.5rem;
        }

        .quick-action-title {
            font-size: 0.8rem;
            font-weight: 600;
            color: #ffffff;
        }

        .quick-action-desc {
            font-size: 0.7rem;
            color: #666;
            line-height: 1.2;
        }

        .chat-section {
            flex: 1;
            display: flex;
            flex-direction: column;
            gap: 0.75rem;
        }

        .chat-messages {
            flex: 1;
            background: #2a2a2a;
            border: 1px solid #333;
            border-radius: 10px;
            padding: 0.75rem;
            overflow-y: auto;
            max-height: 200px;
        }

        .message {
            display: flex;
            gap: 0.5rem;
            margin-bottom: 0.75rem;
        }

        .message-avatar {
            width: 24px;
            height: 24px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 0.8rem;
            flex-shrink: 0;
        }

        .message.user .message-avatar {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .message.ai .message-avatar {
            background: linear-gradient(135deg, #4ade80 0%, #22c55e 100%);
            color: white;
        }

        .message-content {
            flex: 1;
            background: #333;
            padding: 0.5rem 0.75rem;
            border-radius: 8px;
            font-size: 0.8rem;
            line-height: 1.4;
        }

        .message.ai .message-content {
            background: linear-gradient(135deg, #1e3a8a 0%, #1e40af 100%);
        }

        .input-section {
            display: flex;
            gap: 0.5rem;
            align-items: flex-end;
        }

        .mini-input {
            flex: 1;
            background: #2a2a2a;
            border: 1px solid #333;
            border-radius: 8px;
            padding: 0.5rem 0.75rem;
            color: #ffffff;
            font-size: 0.8rem;
            resize: none;
            min-height: 32px;
            max-height: 80px;
        }

        .mini-input:focus {
            outline: none;
            border-color: #667eea;
        }

        .mini-input::placeholder {
            color: #666;
        }

        .send-btn {
            width: 32px;
            height: 32px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s ease;
            color: white;
            font-size: 0.8rem;
        }

        .send-btn:hover {
            transform: translateY(-1px);
            box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
        }

        .voice-btn {
            width: 32px;
            height: 32px;
            background: #333;
            border: 1px solid #444;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s ease;
            color: #666;
            font-size: 0.8rem;
        }

        .voice-btn:hover {
            background: #444;
            color: #ffffff;
        }

        .voice-btn.listening {
            background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
            color: white;
            animation: listening 1s infinite;
        }

        @keyframes listening {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.05); }
        }

        .footer {
            background: #2a2a2a;
            border-top: 1px solid #333;
            padding: 0.75rem 1rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-shrink: 0;
            min-height: 50px;
        }

        .status-indicator {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            font-size: 0.7rem;
            color: #666;
        }

        .status-dot {
            width: 6px;
            height: 6px;
            background: #4ade80;
            border-radius: 50%;
            animation: blink 1.5s infinite;
        }

        @keyframes blink {
            0%, 50% { opacity: 1; }
            51%, 100% { opacity: 0.3; }
        }

        .expand-btn {
            background: #333;
            border: 1px solid #444;
            border-radius: 6px;
            padding: 0.5rem 1rem;
            color: #ffffff;
            font-size: 0.8rem;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .expand-btn:hover {
            background: #444;
        }

        /* 滚动条样式 */
        .chat-messages::-webkit-scrollbar {
            width: 4px;
        }

        .chat-messages::-webkit-scrollbar-track {
            background: #2a2a2a;
        }

        .chat-messages::-webkit-scrollbar-thumb {
            background: #333;
            border-radius: 2px;
        }

        .chat-messages::-webkit-scrollbar-thumb:hover {
            background: #444;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-left">
            <div class="ai-avatar">🤖</div>
            <div class="header-title">AI 助手</div>
        </div>
        <div class="header-actions">
            <div class="action-btn" title="设置" onclick="openSettings()">⚙️</div>
            <div class="action-btn" title="最小化" onclick="minimizeWindow()">➖</div>
            <div class="action-btn" title="关闭" onclick="closeWindow()">✕</div>
        </div>
    </div>

    <div class="main-content">
        <div class="quick-actions">
            <div class="quick-action" onclick="sendQuickMessage('播放音乐')">
                <div class="quick-action-icon">🎵</div>
                <div class="quick-action-title">播放音乐</div>
                <div class="quick-action-desc">智能音乐控制</div>
            </div>
            <div class="quick-action" onclick="sendQuickMessage('写文章')">
                <div class="quick-action-icon">📝</div>
                <div class="quick-action-title">写文章</div>
                <div class="quick-action-desc">AI 内容创作</div>
            </div>
            <div class="quick-action" onclick="sendQuickMessage('系统优化')">
                <div class="quick-action-icon">🔧</div>
                <div class="quick-action-title">系统优化</div>
                <div class="quick-action-desc">智能系统维护</div>
            </div>
            <div class="quick-action" onclick="sendQuickMessage('工作流程')">
                <div class="quick-action-icon">🔄</div>
                <div class="quick-action-title">工作流程</div>
                <div class="quick-action-desc">自动化任务</div>
            </div>
        </div>

        <div class="chat-section">
            <div class="chat-messages" id="chatMessages">
                <div class="message ai">
                    <div class="message-avatar">🤖</div>
                    <div class="message-content">
                        您好！我是您的 AI 助手，有什么可以帮助您的吗？
                    </div>
                </div>
            </div>
            
            <div class="input-section">
                <textarea 
                    class="mini-input" 
                    id="miniInput" 
                    placeholder="输入您的指令..."
                    rows="1"
                ></textarea>
                <button class="voice-btn" id="voiceBtn" onclick="toggleVoice()">🎤</button>
                <button class="send-btn" onclick="sendMessage()">📤</button>
            </div>
        </div>
    </div>

    <div class="footer">
        <div class="status-indicator">
            <div class="status-dot"></div>
            <span id="statusText">AI 在线</span>
        </div>
        <button class="expand-btn" onclick="openMainWindow()">打开主界面</button>
    </div>

    <script>
        const { ipcRenderer } = require('electron');

        let isListening = false;
        let recognition = null;

        // 初始化
        document.addEventListener('DOMContentLoaded', () => {
            updateStatus('AI 系统已就绪');
            setupEventListeners();
            initializeSpeechRecognition();
        });

        // 设置事件监听器
        function setupEventListeners() {
            const miniInput = document.getElementById('miniInput');
            const sendBtn = document.querySelector('.send-btn');

            // 回车发送消息
            miniInput.addEventListener('keydown', (e) => {
                if (e.key === 'Enter' && !e.shiftKey) {
                    e.preventDefault();
                    sendMessage();
                }
            });

            // 自动调整输入框高度
            miniInput.addEventListener('input', () => {
                miniInput.style.height = 'auto';
                miniInput.style.height = Math.min(miniInput.scrollHeight, 80) + 'px';
            });
        }

        // 初始化语音识别
        function initializeSpeechRecognition() {
            if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) {
                const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
                recognition = new SpeechRecognition();
                recognition.continuous = false;
                recognition.interimResults = false;
                recognition.lang = 'zh-CN';

                recognition.onstart = () => {
                    isListening = true;
                    updateVoiceButton('listening');
                    updateStatus('正在听取语音...');
                };

                recognition.onresult = (event) => {
                    const transcript = event.results[0][0].transcript;
                    document.getElementById('miniInput').value = transcript;
                    updateStatus('语音识别完成');
                };

                recognition.onerror = (event) => {
                    console.error('语音识别错误:', event.error);
                    updateStatus('语音识别出错');
                    updateVoiceButton('idle');
                };

                recognition.onend = () => {
                    isListening = false;
                    updateVoiceButton('idle');
                    updateStatus('AI 系统已就绪');
                };
            }
        }

        // 切换语音录制
        function toggleVoice() {
            if (!recognition) {
                updateStatus('语音识别不可用');
                return;
            }

            if (isListening) {
                recognition.stop();
            } else {
                recognition.start();
            }
        }

        // 更新语音按钮状态
        function updateVoiceButton(state) {
            const button = document.getElementById('voiceBtn');
            button.className = `voice-btn ${state}`;
        }

        // 更新状态文本
        function updateStatus(message) {
            document.getElementById('statusText').textContent = message;
        }

        // 发送消息
        function sendMessage() {
            const input = document.getElementById('miniInput');
            const message = input.value.trim();
            
            if (!message) return;

            addMessage('user', message);
            input.value = '';
            input.style.height = 'auto';

            // 模拟 AI 响应
            setTimeout(() => {
                const responses = [
                    '正在为您处理...',
                    '好的，我来帮您执行这个任务',
                    'AI 正在分析您的需求...',
                    '已收到您的指令，正在处理中...'
                ];
                const randomResponse = responses[Math.floor(Math.random() * responses.length)];
                addMessage('ai', randomResponse);
            }, 1000);
        }

        // 快速消息
        function sendQuickMessage(message) {
            document.getElementById('miniInput').value = message;
            sendMessage();
        }

        // 添加消息
        function addMessage(sender, text) {
            const messagesContainer = document.getElementById('chatMessages');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${sender}`;
            
            const avatar = sender === 'user' ? '👤' : '🤖';

            messageDiv.innerHTML = `
                <div class="message-avatar">${avatar}</div>
                <div class="message-content">${text}</div>
            `;

            messagesContainer.appendChild(messageDiv);
            messagesContainer.scrollTop = messagesContainer.scrollHeight;
        }

        // 打开主窗口
        function openMainWindow() {
            ipcRenderer.invoke('show-main-window');
        }

        // 窗口控制函数
        function minimizeWindow() {
            ipcRenderer.invoke('hide-mini-window');
        }

        function closeWindow() {
            ipcRenderer.invoke('hide-mini-window');
        }

        function openSettings() {
            // 这里可以添加设置功能
            console.log('打开设置');
        }
    </script>
</body>
</html>
